<<<<<<< HEAD
//负责人：副组长
package com.java.view;
import com.java.model.Student;
import com.java.datamanage.StudentManageSystem;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;


import java.util.Map;

// 学生管理面板
class StudentPanel extends JPanel {
    private final StudentManageSystem system;
    // 学生管理系统实例，用于获取学生数据和执行相关操作

    // 用于展示学生信息的表格2
    private JTable studentTable;
    //学生表格的数据模型，用于管理表格的表头和数据
    private DefaultTableModel studentTableModel;
    //构造函数，初始化学生管理面板
    public StudentPanel(StudentManageSystem system) {
        this.system = system;
        initUI();
    }

    private void initUI() {
        //setLayout(new BorderLayout());
        setLayout(new BorderLayout());

        // 定义表格的表头
        String[] columnNames = {"学号", "姓名", "性别", "年龄", "班级", "成绩", "gpa", "政治面貌"};
        // 创建表格数据模型，传入表头和初始行数 0
        studentTableModel = new DefaultTableModel(columnNames, 0) {
            /**
             * 重写该方法，设置表格单元格不可编辑
             * @param row 行索引
             * @param column 列索引
             * @return 始终返回 false，表示单元格不可编辑
             */
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        // 使用上述创建的数据模型初始化表格，表格将根据数据模型显示内容
        studentTable = new JTable(studentTableModel);

        // 设置表格的选择模式为单选模式，即用户只能选择一行
        studentTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        // 为表格的选择模型添加列表选择监听器，当表格行选择状态发生变化时触发事件
        studentTable.getSelectionModel().addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                showStudentGradeDetails();
            }
        });

        JScrollPane scrollPane = new JScrollPane(studentTable);
        add(scrollPane, BorderLayout.CENTER);

        // 按钮面板
=======
//负责人：
package com.java.view;

// 学生管理面板

        //表头
        //String[] columnNames = {"学号", "姓名", "性别", "年龄", "班级", "绩点"，"政治面貌","职位"};



        /*按钮面板
>>>>>>> 1985484ee1452c7d22a8520a807bdae2011c4a07
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));

        JButton addButton = new JButton("添加学生");
        addButton.addActionListener(e -> showAddStudentDialog());

        JButton editButton = new JButton("编辑学生");
        editButton.addActionListener(e -> showEditStudentDialog());

        JButton deleteButton = new JButton("删除学生");
        deleteButton.addActionListener(e -> deleteSelectedStudent());

        JButton gradeButton = new JButton("管理成绩");
        gradeButton.addActionListener(e -> showManageGradesDialog());

        buttonPanel.add(addButton);
        buttonPanel.add(editButton);
        buttonPanel.add(deleteButton);
        buttonPanel.add(gradeButton);
<<<<<<< HEAD
        // 将按钮面板添加到当前面板的南部区域
        add(buttonPanel, BorderLayout.SOUTH);

        // 初始加载数据，调用 refresh 方法更新表格显示
        refresh();
    }
    /**
     * 刷新学生表格数据的方法，可在数据发生变化时调用以更新界面显示
     */
    public void refresh() {
        studentTableModel.setRowCount(0);//清空表格数据
        for (Student s : system.getAllStudents()) {
            StringBuilder scoresA = new StringBuilder();
            for (Map.Entry<String, Double> entry : s.getScores().entrySet()) {
                scoresA.append(entry.getKey())
                        .append(":").append(entry.getValue())
                        .append(" ");
            }
            studentTableModel.addRow(new Object[]{
                    s.getName(),//姓名
                    s.getId(),//学号
                    s.getAge(),//年龄
                    s.getClassName(),//班级
                    s.getGrade(),//年级
                    scoresA,//成绩
                    s.getGpa(),//绩点
                    s.getPoliticalStatus(),//政治面貌
            });
        }
    }

    /**
     * 显示选中学生成绩详情的方法，当表格行选择状态变化时触发
     */
    private void showStudentGradeDetails() {
        int selectedRow = studentTable.getSelectedRow();
        if (selectedRow == -1) return;
        Student selectedStudent = system.getStudentById((String)
                studentTableModel.getValueAt(selectedRow, 0));
        if (selectedStudent == null) return;

        StringBuilder details = new StringBuilder("学生成绩详情：\n");
        for (Map.Entry<String, Double> entry : selectedStudent.getScores().entrySet()) {
            details.append(entry.getKey())
                    .append(": ")
                    .append(entry.getValue()).append("\n");
        }
        JOptionPane.showMessageDialog(this, details.toString(),
                "学生成绩详情", JOptionPane.INFORMATION_MESSAGE);
    }

    /**
     * 显示添加学生对话框的方法，点击“添加学生”按钮时调用
     */
    private void showAddStudentDialog() {
        JTextField idField = new JTextField();
        JTextField nameField = new JTextField();
        JTextField ageField = new JTextField();
        JTextField classNameField = new JTextField();
        JTextField gradeField = new JTextField();
        JTextField politicalStatusField = new JTextField();
        JTextField gpaField = new JTextField();

        JPanel panel = new JPanel();
        panel.add(new JLabel("学号:"));//ID
        panel.add(idField);
        panel.add(new JLabel("姓名:"));//Name
        panel.add(nameField);
        panel.add(new JLabel("年龄:"));//Age
        panel.add(ageField);
        panel.add(new JLabel("班级:"));//ClassName
        panel.add(classNameField);
        panel.add(new JLabel("年级"));//Grade
        panel.add(gradeField);
        panel.add(new JLabel("绩点"));//GPA
        panel.add(gpaField);
        panel.add(new JLabel("政治面貌:"));//PoliticalStatusField
        panel.add(politicalStatusField);

        if (JOptionPane.showConfirmDialog(
                this, panel, "添加学生",
                JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
            try {

                String id = idField.getText();
                String name = nameField.getText();
                int age = Integer.parseInt(ageField.getText());
                String className = classNameField.getText();
                String grade = gradeField.getText();
                int gpa = Integer.parseInt(gpaField.getText());
                String politicalStatus = politicalStatusField.getText();

                Student newStudent = new Student(id,name,grade,age,0.0,className, politicalStatus);
                system.addStudent(newStudent);
                refresh();
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "请输入有效的年龄。", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    /**
     * 显示编辑学生信息对话框的方法，点击“编辑学生”按钮时调用
     */

    private void showEditStudentDialog() {
        int selectedRow = studentTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(
                    this, "请先选中一行", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
        Student selectedStudent = system.getStudentById((String)
                studentTableModel.getValueAt(selectedRow, 0));
        if (selectedStudent == null) return;


        JTextField NameField = new JTextField(selectedStudent.getName());//Name
        JTextField AgeField = new JTextField(String.valueOf(selectedStudent.getAge()));//Age
        JTextField ClassNameField = new JTextField(selectedStudent.getClassName());//ClassName
        JTextField GradeField = new JTextField(selectedStudent.getGrade());//Grade
        JTextField PoliticalStatusField = new JTextField(selectedStudent.getPoliticalStatus());//政治面貌
        JTextField IdFile = new JTextField(selectedStudent.getId());//ID
        JTextField GPAFile = new JTextField(String.valueOf(selectedStudent.getGpa()));//GPA


        JPanel panel = new JPanel();
        panel.add(new JLabel("学号"));
        panel.add(IdFile);
        panel.add(new JLabel("姓名:"));
        panel.add(NameField);
        panel.add(new JLabel("性别:"));
        panel.add(GradeField);
        panel.add(new JLabel("年龄:"));
        panel.add(AgeField);
        panel.add(new JLabel("绩点"));
        panel.add(GPAFile);
        panel.add(new JLabel("班级:"));
        panel.add(ClassNameField);
        panel.add(new JLabel("政治面貌:"));
        panel.add(PoliticalStatusField);

        if (JOptionPane.showConfirmDialog(this, panel, "编辑学生", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
            try {

                String name = NameField.getText();
                int age = Integer.parseInt(AgeField.getText());
                String className = ClassNameField.getText();
                String grade = GradeField.getText();
                String politicalStatus = PoliticalStatusField.getText();

                Student updatedStudent = new Student(selectedStudent.getId(), name, grade, age, selectedStudent.getGpa(), className, politicalStatus);
                system.updateStudent(updatedStudent);
                refresh();
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(
                        this, "请输入有效的年龄!", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    /**
     * 删除选中学生信息的方法，点击“删除学生”按钮时调用
     */

    private void deleteSelectedStudent() {
        int idx = studentTable.getSelectedRow();
        if (idx == -1) return;
        String id = (String) studentTableModel.getValueAt(idx, 0);
        int ans = JOptionPane.showConfirmDialog(
                this, "确定删除学生 " + id + "吗？", "确认", JOptionPane.YES_NO_OPTION);
        if (ans == JOptionPane.YES_OPTION) {
            system.deleteStudent(id);
            refresh();
        }
    }
    /**
     * 显示管理学生成绩对话框的方法，点击“管理成绩”按钮时调用
     */
    private void showManageGradesDialog() {
        int idx = studentTable.getSelectedRow();
        if (idx == -1) return;
        String id = (String) studentTableModel.getValueAt(idx, 0);
        Student stu = system.getStudentById(id);
        if (stu == null) return;

        JFrame f = new JFrame("管理成绩 :" + stu.getName());
        JTextArea area = new JTextArea(10, 30);
        area.setEditable(false);
        updateScoreArea(area, stu);

        JButton addBtn = new JButton("添加/修改成绩");
        addBtn.addActionListener(e -> {
            String course = JOptionPane.showInputDialog(f, "课程名:");
            if (course == null || course.isBlank()) return;
            String gradeStr = JOptionPane.showInputDialog(f, "成绩:");
            if (gradeStr == null) return;
            try {
                double grade = Double.parseDouble(gradeStr);
                stu.addOrUpdateCourseGrade(course, grade);
                updateScoreArea(area, stu);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(f, "请输入合理数字！");
            }
        });
        JButton delBtn = new JButton("删除成绩");
        delBtn.addActionListener(e -> {
            String course = JOptionPane.showInputDialog(f, "课程名:");
            if (course == null || course.isBlank()) return;
            stu.removeCourseGrade(course);
            updateScoreArea(area, stu);
        });
        JButton closeBtn = new JButton("关闭");
        closeBtn.addActionListener(e -> f.dispose());

        JPanel btnPanel = new JPanel();
        btnPanel.add(addBtn); btnPanel.add(delBtn); btnPanel.add(closeBtn);

        f.add(new JScrollPane(area), BorderLayout.CENTER);
        f.add(btnPanel, BorderLayout.SOUTH);
        f.setSize(370, 270);
        f.setLocationRelativeTo(this);
        f.setVisible(true);
    }
    private void updateScoreArea(JTextArea area, Student stu) {
        area.setText("");
        for (Map.Entry<String, Double> e : stu.getScores().entrySet())
            area.append(e.getKey() + ":" + e.getValue() + "\n");
    }
}
=======
        add(buttonPanel, BorderLayout.SOUTH);

    */

    //初始加载数据

   //public void refresh() {}


>>>>>>> 1985484ee1452c7d22a8520a807bdae2011c4a07

